from django.contrib.auth.models import User
from django.db import models
from utils.modelsMiXin import ModelSetMiXin

# Create your models here.
class UserDetail(ModelSetMiXin):  #个人详情信息
    SEX_CHOICES = (
        (0,'女'),
        (1,'男')
    )

    avatar = models.TextField(null=True,blank=True,verbose_name='头像')
    phone = models.CharField(null=True,blank=True,verbose_name='手机号',max_length=11,unique=True)
    age = models.IntegerField(null=True,blank=True,verbose_name='年龄')
    sex = models.IntegerField(null=True,blank=True,verbose_name='性别',choices=SEX_CHOICES)
    birthday = models.DateField(null=True,blank=True,verbose_name='生日')

    user = models.OneToOneField(User,on_delete=models.DO_NOTHING) #这是一对一，Foreign是一对多，Mang为多对多
                                                #DO_NOTHING不能进行任何操作；CASCADE关联表一起删除(不用，因为不会真正删除)，
    class Meta:  #显示信息
        db_table = 'user_detail' #数据库中表名
        verbose_name = '用户详情'
        verbose_name_plural = verbose_name  #反向查询名字

class Area(models.Model):
    name = models.CharField(max_length=20,verbose_name='名称')
    parent = models.ForeignKey('self',on_delete=models.SET_NULL,null=True,blank=True,verbose_name='上级行政区划') #自己去关联自己

    class Meta:
        db_table = 'area'
        verbose_name = '行政区划'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.name}'

class Address(ModelSetMiXin):
    name = models.CharField(max_length=40,verbose_name='地址名')
    receiver = models.CharField(max_length=20,verbose_name='收货人')
    #因为省，市，区，都是用的Area这个表，如果后面反向查询的时候Area_set会冲突,因为有三个分不清楚，所以给每个字段设置一个反向查询的新名字，related_name=''.
    province = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='省',related_name='province_address') #PROTECT表示保护，不能进行任何更改，这是省份啥的
    city = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='市',related_name='city_address')
    district = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='区',related_name='district_address')
    place = models.CharField(max_length=40,verbose_name='详情地址')
    mobile = models.CharField(max_length=11,verbose_name='手机')

    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')

    class Meta:
        ordering = ['-update_time']
        db_table = 'address'
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name


